#!/usr/bin/python
import sys
import re


########################################################################
#  This funtion abstract all .cpp-file form vcproj-file, and make a 
#  couple of obj-file and src-file, eg. obj2src[aaa.o] = aaa.cpp
########################################################################
def abstract_obj2src_map_from_vcproj(vcproj):
	file = open(vcproj, 'r')
	obj2src = dict()
	while True:
		line = file.readline()
		
		# zero length indicates EOF
		if len(line) == 0:
			break
		
		# only care cpp-file
		if not re.search("\.cpp", line):
			continue;
		
		# one line is divided into 3 parts by '"', only the middle part is useful
		src_file_path = line.split("\"")[1]
		
		# transform the path format from the window to linux, repalce '\' with '/'
		src_file_path = src_file_path.replace('\\', '/')
		
		# get the file name, the last sub string divided by '/'
		src_file_name = src_file_path.split('/')[-1]

		# obj-file name, repalce '.cpp' with '.o'
		obj_file_name = src_file_name.replace('.cpp', '.o')
		
		# make a obj-file and src-file pair
		obj2src[obj_file_name] = src_file_path

	file.close()
	return obj2src


########################################################################
#  This funtion abstract all directories which contain .h-file 
########################################################################
def abstract_include_dir_from_vcproj(vcproj):
	file = open(vcproj, 'r')
	include_dirs = set()
	while True:
		line = file.readline()
		
		# zero length indicates EOF
		if len(line) == 0:
			break

		# only care h-file
		if not re.search("\.h", line):
			continue;

		# one line is divided into 3 parts by '"', only the middle part is useful
		include_file_path = line.split("\"")[1]

		# transform the path format from the window to linux, repalce '\' with '/'
		include_file_path = include_file_path.replace('\\', '/')

		# remove the file name, just leave the dir path
		include_dir_path = include_file_path[0 : include_file_path.rfind('/')]

		# add into include-dir set
		include_dirs.add(include_dir_path)
	
	file.close()
	return include_dirs


########################################################################
#  platform related variables
########################################################################
CROSS = 'arm-linux-'
AR = 'ar'
RM = 'rm -rf'
CPP = 'g++'
#CFLAGS = '-lpthread -frtti -fexceptions -fopenmp -Werror -Wformat -Wno-deprecated -g -O2'
CFLAGS = '-lpthread -frtti -fexceptions -fPIC -fstack-protector-all -Werror -Wformat -Wno-deprecated -g -O2 -fno-builtin-memcpy -fno-builtin-memmove'
INCS = ''
LIBS = '-lpthread -lopenmp -L/usr/lib/mysql -lmysqlclient'
MACROS = ''



########################################################################
#  main routing
########################################################################
if len(sys.argv) < 5:
	print >> sys.stderr, 'usage: <vcproj_file> <target_name> <target_type> <include_dirs> <predefined_macros> <temp_path>'
	sys.exit(1)

vcproj_file = sys.argv[1]
target_name = sys.argv[2]
target_type = sys.argv[3]
include_dirs = sys.argv[4].split(';')

# add predefined macros
#if len(sys.argv) == 7:
predefined_macros = sys.argv[5].split(';')
for macro in predefined_macros:
	MACROS += ' -D' + macro

temp_path = sys.argv[6]

# add include-dirs
include_dirs += abstract_include_dir_from_vcproj(vcproj_file)
for dir in include_dirs:
	INCS += ' -I' + dir

# build .o file to .cpp file map
obj2src = abstract_obj2src_map_from_vcproj(vcproj_file)

# final target
print '%s: ' %(target_name),
for obj in obj2src.keys():
	print temp_path+'%s' % (obj),

if target_type == 'executable':
	print '\n\t%s%s %s %s %s %s $^ -o $@\n' % (CROSS,CPP, CFLAGS, MACROS, INCS, LIBS)
elif target_type == 'static':
	print '\n\t%s%s -qcs $@ $^ \n' % (CROSS,AR)
else:
	print >> sys.stderr, 'Unknown target_type'
	sys.exit(1)


# object files targets
for obj in obj2src.keys():
	print temp_path+'%s: %s' % (obj, obj2src[obj])
	print '\t%s%s %s %s %s -c $< -o $@\n' % (CROSS,CPP, CFLAGS, MACROS, INCS)


# clean target
print 'clean:\n\t%s %s*.o %s' % (RM,temp_path,target_name)
